package com.atguigu.bigdata.chapter05.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2022/9/2 10:26
 */
public class Flink04_FlatMap {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        //
        env
            .fromElements(1, 10, 3, 6, 9)
            .flatMap(new FlatMapFunction<Integer, Integer>() {
                @Override
                public void flatMap(Integer value,
                                    Collector<Integer> out) throws Exception {
                    // 每调用一次, 后面流中就会添加一个元素
    
    
                    if (value % 2 == 0) {
    
                        out.collect(value * value);
                        out.collect(value * value * value);
                    }
                }
            })
            .print();
            
        
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
map: 1->1
filter: 1->0/1
flatMap: 1->N  (N>=0)

flatMap: 理论上可以替换掉map和filter

 */